package com.shrio.word;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.Version;

import javax.servlet.ServletOutputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExportWordMergeTest {

    public static void main(String[] args) {
        Configuration config = new Configuration(new Version("2.3.0"));
        config.setDefaultEncoding("utf-8");
        Writer writer = null;
        FileInputStream fis = null;
        ServletOutputStream out = null;
        try {
            config.setDirectoryForTemplateLoading(new File("E:/word"));
            config.setObjectWrapper(new DefaultObjectWrapper());
            Template template = config.getTemplate("preTempMerge.ftl", "UTF-8");
            Map root = new HashMap();
            root.put("title", "2023-06-07至2023-06-08降水");
            List<StationData> list = new ArrayList<StationData>();
            list.add(new StationData("广州", "天河", "0.3"));
            list.add(new StationData("广州", "白云", "0.4"));
            list.add(new StationData("广州", "东山", "0.5"));

            list.add(new StationData("深圳", "南山", "0.4"));
            list.add(new StationData("深圳", "福田", "0.3"));

            list.add(new StationData("佛山", "禅城", "0.5"));
            root.put("dataList", list);
            File file = new File("E:/word/降水Merge.docx");
            if(!file.exists()){
                file.createNewFile();
            }else {
                file.delete();
                file.createNewFile();
            }
            writer = new BufferedWriter(new FileWriter(file));
            template.process(root, writer);
            writer.flush();
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
